﻿using System;
using moc.IDAL;
using moc.IBLL;
using moc.Models;
using System.Linq;

namespace moc.BLL
{
    public partial class CustomerLoginBll :ICustomerLoginBll
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly ICustomerLoginDal _dal;
        public CustomerLoginBll(IUnitOfWork unitOfWork,ICustomerLoginDal dal)
        {
            _unitOfWork = unitOfWork;
            _dal = dal;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool EditPwd(string password)
        {
            CustomerLogin login = new CustomerLogin()
            {
                Password = password
            };
            _dal.Update(login);
            return (_unitOfWork.SaveChanges() > 0);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<CustomerLogin> GetCustomer(Guid id)
        {
            return _dal.LoadEntities(u => u.Customer_id == id);
        }

        /// <summary>
        /// 检查用户名是否已存在
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public bool IsExist(string loginName)
        {
            return (_dal.LoadEntities(u => u.Login_name == loginName) == null);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public CustomerLogin Login(string loginName, string password)
        {
             return _dal.LoadEntities(u => u.Login_name == loginName && u.Password == password).FirstOrDefault();
        }
       
        /// <summary>
        /// 用户注销
        /// </summary>
        /// <param name="entity"></param>
        public void LoginOut(CustomerLogin entity)
        {
            entity.Status = 0;
            _dal.Update(entity);
            _unitOfWork.SaveChanges();
        }
    }
}
